from rest_framework import serializers
from main.task.models import Task
from utils.serializers import CustomModelSerializer
from system.project.models import Channel
from utils.tools import match_date


class TaskSerializer(CustomModelSerializer):
    """
    排期任务 序列化器
    """

    class Meta:
        model = Task
        fields = '__all__'


class TaskCreateUpdateSerializer(CustomModelSerializer):
    """
    排期任务 序列化器
    """

    def create(self, validated_data):
        validated_data['date'] = match_date(validated_data['date_time'])[0]
        validated_data['project'] = validated_data['channel'].project.id
        return super().create(validated_data)

    class Meta:
        model = Task
        fields = '__all__'
        extra_kwargs = {
            'project': {
                'required': False,
            },
            'date': {
                'required': False,
            },
        }
